package io.modifier.basic.compose.widget.comm

import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.wquick.base.R
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.theme.color
import io.modifier.basic.compose.theme.size
import io.modifier.basic.compose.theme.weight
import io.modifier.basic.compose.widget.weui.core.ui.components.switch.WeSwitch
import io.modifier.basic.emo.ui.core.modifier.throttleClick

@Composable
fun MenuDivider() {
    Box(
        Modifier
            .padding(start = 60.dp)
            .fillMaxWidth()
            .height(1.dp)
            .background(Color(0xffBAC0C7).copy(alpha = .14f))
    )
}

@Composable
fun MenuItem(
    icon: Int, title: String, rightPart: (@Composable () -> Unit)? = null,
    rightText: String = "", showRightArrow: Boolean = true,
    shape: Shape = RoundedCornerShape(0.dp),
    onClick: () -> Unit
) {
    Row(
        Modifier
            .fillMaxWidth()
            .aspectRatio(360f / 52F)
            .background(AppThemeHolder.colors.bgLight, shape)
            .clip(shape)
            .throttleClick { onClick() }
            .padding(AppThemeHolder.config.gap),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painterResource(icon),
            contentDescription = null,
            contentScale = ContentScale.Inside,
            modifier = Modifier.size(28.dp)
        )

        H(16)

        Text(
            title,
            style = AppThemeHolder.textStyle.default.size(17).weight(400)
                .color(AppThemeHolder.colors.txtColor)
        )

        HS()

        rightPart?.invoke()

        if (rightText.isNotBlank()) Text(
            rightText,
            style = AppThemeHolder.textStyle.default.size(15).weight(400)
                .color(AppThemeHolder.colors.txtGray)
        )



        if (showRightArrow) {
            H(13)

            Image(
                painterResource(R.drawable.svg_arrow_right),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .width(5.dp)
                    .height(10.dp)
            )
        }
    }
}

@Composable
fun MenuItemSwitch(
    icon: Int, title: String, enable: Boolean, onChange: (Boolean) -> Unit
) {
    Row(
        Modifier
            .fillMaxWidth()
            .aspectRatio(360f / 52F)
            .background(AppThemeHolder.colors.bgLight)
            .padding(AppThemeHolder.config.gap),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painterResource(icon),
            contentDescription = null,
            contentScale = ContentScale.Inside,
            modifier = Modifier.size(28.dp)
        )

        H(16)

        Text(
            title,
            style = AppThemeHolder.textStyle.default.size(17).weight(400)
                .color(AppThemeHolder.colors.txtColor)
        )

        HS()

        WeSwitch(
            checked = enable,
            onChange = {
                onChange(it)
            }
        )
    }
}

@Composable
fun MenuItemInput(
    value: String,
    hint: String = "",
    onChange: (String) -> Unit
) {
    Row(
        Modifier
            .fillMaxWidth()
            .aspectRatio(360f / 52F)
            .background(AppThemeHolder.colors.bgLight)
            .padding(AppThemeHolder.config.gap),
        verticalAlignment = Alignment.CenterVertically
    ) {

        BasicTextField(
            modifier = Modifier
                .align(Alignment.CenterVertically)
                .fillMaxSize(),
            singleLine = true,
            textStyle = AppThemeHolder.textStyle.default.size(16).weight(400)
                .color(AppThemeHolder.colors.txtColor),
            value = value,
            onValueChange = {
                onChange(it)
            },
            decorationBox = { innerTextField ->
                Row(
                    modifier = Modifier,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Box(contentAlignment = Alignment.CenterStart) {
                        if (value.isEmpty()) {
                            Text(
                                text = hint,
                                color = AppThemeHolder.colors.txtGray,
                                fontSize = 14.sp,
                                textAlign = TextAlign.Start
                            )
                        }
                        innerTextField()
                    }
                }
            }
        )
    }
}


@Composable
private fun PreviewMenuItem() {
    AppTheme {
        Column(
            Modifier
                .fillMaxSize()
                .background(Color.Black)
        ) {
            MenuItem(R.drawable.svg_return_white, "Nickname", rightText = "Bob", showRightArrow = true,
                onClick = {

                })
            MenuDivider()

            MenuItemSwitch(R.drawable.svg_return_white, "Nickname", enable = false, onChange = {

            })
            MenuDivider()

            val inputStr = remember { mutableStateOf("") }
            MenuItemInput(inputStr.value, "请输入...", onChange = {
                inputStr.value = it
            })
            MenuDivider()

        }


    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkMenuItemP() {
    PreviewMenuItem()
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightMenuItemP() {
    PreviewMenuItem()
}